<?php


namespace App\Controller\App;


use App\Base\BaseController4App;
use App\Repository\BannerRepository;
use App\Repository\CommentTextRepository;
use App\Repository\ConfigRepository;
use App\Repository\OrdersRepository;
use App\Repository\PicRepository;
use App\Repository\SalesCutRepository;
use App\Repository\SiteOilCategoryRepository;
use App\Repository\SiteRepository;
use App\Service\Wechat\WechatLogin;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/app")
 */
class AppIndexController extends BaseController4App
{
    /**
     * @Route("/config", name="AppConfig")
     */
    public function AppConfig(
        ConfigRepository $configRepository,
        PicRepository $picRepository
    )
    {
        $data["charge"] = $configRepository->getKVNumArray("CHARGE");
        $data["phone"] = $configRepository->getByK("PHONE");

        //海报图
        $list = $picRepository->findAll();
        foreach ($list as $row) {
            $data[$row->getCode()] = $row->getContent();
        }


        return $this->json_out(1,"OK",$data);
    }
    /**
     * @Route("/index", name="AppIndex")
     */
    public function AppIndex(BannerRepository $bannerRepository,SiteRepository $siteRepository)
    {
//        $data = $this->getRequestData();
        $data["banners"] = $bannerRepository->findBy(array("enable"=>true),array("sort"=>"ASC"));
        $data["site"] = $siteRepository->findAll()[0];
        return $this->json_out(1,"OK",$data);
    }
    /**
     * @Route("/prepare", name="AppPrepare")
     */
    public function AppPrepare(
        SiteRepository $siteRepository
        ,SalesCutRepository $salesCutRepository
        ,SiteOilCategoryRepository $siteOilCategoryRepository
    )
    {
        //加油站数据
        $data["site"] = $siteRepository->findAll();
        //满减
        $data["cut"] = $salesCutRepository->findBy(array("isUse"=>true));
        //商品数据
        $data["product"] = $siteOilCategoryRepository->findAll();
        $this->setGroups("list","cut","site");

        return $this->json_out(1,"OK",$data);
    }


    /**
     * @Route("/comment", name="AppCommentText")
     */
    public function AppCommentText(
        CommentTextRepository $commentTextRepository
    )
    {
        $data = $commentTextRepository->findBy(array(),array("sort"=>"ASC"));
        return $this->json_out(1,"OK",$data);
    }


    /**
     * @Route("/cur/order", name="AppCurOrder")
     */
    public function AppCurOrder(
        OrdersRepository $ordersRepository
    )
    {
        $data = $ordersRepository->findBy(array(),array("id"=>"DESC"),10,0);
        $this->setGroups("user","order");
        return $this->json_out(1,"OK",$data);
    }

    /**
     * @Route("/get/openid", name="getOpenid")
     */
    public function getOpenid(
        WechatLogin $wechatLogin
    )
    {
        $req = $this->getRequestData();
        $code = $req["code"]??"";
        $wechatLogin->getOpenidAndAccessToken4MiniApp($code);

        return $this->json_out(1,"OK",$wechatLogin->Openid);
    }
}